Imports DAL
Imports BAL
Imports System
Imports MyClasses
Imports System.Text

Public Class frmPI
    Dim dblTotSchmDisc As Double
    Dim dblTotalTax As Double
    Public dblScheme As Double
    Public dblAmt As Double
    Public DiscLessAmt As Double
    Public PurItem As New CPurchaseItem
    Public ItemTaxes() As TaxPI
    Public ItemScheme As CScheme
    Public Status As Char   'C:- Close button is clicked  'O: OK Button  is clicked
    Public MarginStatus As Char 'T: Margin window is Open   F: Margin window is Close
    Public ItemID As Integer
    Public VoucherType As String
    Public dtbDefltTaxID As DataTable

#Region "FORM EVENTS & CONTROL EVENTS"
    Private Sub txtTax_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTax.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                SendKeys.Send("{TAB}")
            ElseIf Trim(CDbl(txtRate1.Text)) <= 0 Then
                e.SuppressKeyPress = True
                BCommon.ShowMsgError(10116)
                txtTax.Text = "0.00"
                txtRate1.Focus()
            Else
                e.SuppressKeyPress = True
                Call SetDiscLesAmt()
                CalculateTaxes()
                AssignTaxToGrid()
                ShowTaxGrid()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtTax_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtDisc_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDisc.GotFocus
        Try
            If Trim(txtDisc.Text) <> String.Empty AndAlso CDbl(txtDisc.Text) = 0 Then txtDisc.Text = String.Empty

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtDisc_GotFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtDisc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDisc.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                SendKeys.Send("{TAB}")
            End If

            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End If
            If BCommon.ValidateText(txtDisc.Text, e.KeyCode, "NUM") = False Then e.SuppressKeyPress = True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtDisc_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub frmPI_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Try
            If VoucherType = "DP" Then
                If e.Control = True Then
                    If e.KeyCode = Keys.F Then      'Display Top 3 Margins    
                        Call Display_Margins()
                    ElseIf e.KeyCode = Keys.S Then      'Display Scheme Selection Form
                        If ItemScheme.SchemeID = 0 Then
                            Call Show_Scheme()
                        End If
                    End If
                ElseIf e.KeyCode = Keys.Escape Then '
                    If MarginStatus = "T" Then
                        Call btnCancel1_Click(Nothing, Nothing)
                    ElseIf CanFormClose() = True Then
                        Call btnClose_Click(Nothing, Nothing)
                    End If
                End If
            Else
                If e.Control = True Then
                    If e.KeyCode = Keys.F Then      'Display Top 3 Margins    
                        Call Display_Margins()
                    ElseIf e.KeyCode = Keys.S Then      'Display Scheme Selection Form
                        Call Show_Scheme()
                    End If
                ElseIf e.KeyCode = Keys.Escape Then '
                    If MarginStatus = "T" Then
                        Call btnCancel1_Click(Nothing, Nothing)
                    ElseIf CanFormClose() = True Then
                        Call btnClose_Click(Nothing, Nothing)
                    End If
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmPI_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Public Sub frmPI_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.Location = New Point(frmNewPI.cmbPayMode.Left - 42, frmNewPI.fgItems.Top + 62)
            Call BCommon.SetFormStandards(Me)
            Call setControls()
            SetTaxGrid()
            Call DisplayData()
            If IsNothing(ItemTaxes) OrElse ItemTaxes.Length = 0 Then SetDefualtTaxes()
            cmbMfg.CustomFormat = "MMM/yy"
            cmbExp.CustomFormat = "MMM/yy"
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmPI_Load")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtBatchno_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBatchno.KeyDown
        Dim dtbTbatch As DataTable
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub

            If e.KeyCode = Keys.Enter Then
                If Trim(txtBatchno.Text) <> String.Empty Then
                    dtbTbatch = DPurchaseInvoice.BatchDetail(ItemID, txtBatchno.Text)
                    If dtbTbatch.Rows.Count > 0 Then
                        txtMrp1.Enabled = False
                        cmbMfg.Value = dtbTbatch.Rows(0).Item(0)
                        cmbMfg.CustomFormat = "MMM/yy"
                        cmbMfg.Enabled = False
                        cmbExp.Value = dtbTbatch.Rows(0).Item(1)
                        cmbExp.CustomFormat = "MMM/yy"
                        cmbExp.Enabled = False
                        txtMrp1.Text = Format(dtbTbatch.Rows(0).Item(2), "0.00")
                    Else
                        txtMrp1.Enabled = True
                        cmbMfg.Enabled = True
                        cmbExp.Enabled = True
                    End If
                End If
                SendKeys.Send("{TAB}")
            End If
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "USERID") = False Then e.SuppressKeyPress = True

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtBatchno_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtqty_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtqty.GotFocus
        Try
            If Trim(txtqty.Text) <> String.Empty AndAlso CDbl(txtqty.Text) = 0 Then txtqty.Text = String.Empty

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtqty_GotFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtqty_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtqty.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")

            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "QTY") = False Then e.SuppressKeyPress = True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtqty_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtScheme_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtScheme.GotFocus
        Try
            If Trim(txtScheme.Text) <> String.Empty AndAlso CDbl(txtScheme.Text) = 0 Then txtScheme.Text = String.Empty

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtScheme_GotFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtScheme_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtScheme.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
            If Not IsNothing(ItemScheme) AndAlso ItemScheme.schemeItemID <= 0 Then
                e.SuppressKeyPress = True
                Me.Cursor = Cursors.Default
                Exit Sub
            End If
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End If
            If BCommon.ValidateText(txtScheme.Text, e.KeyCode, "NUM") = False Then e.SuppressKeyPress = True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtScheme_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtDispSchem_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDispSchem.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                SendKeys.Send("{TAB}")
                Me.Cursor = Cursors.Default
                Exit Sub
            ElseIf e.KeyCode = Keys.Delete Then
                ItemScheme = Nothing
                txtDispSchem.Text = ""
                txtScheme.Text = "0"
                CalculateTaxes()
            End If
            e.SuppressKeyPress = True
            '            If PurItem.ItemID > 0 Then Call Show_Scheme()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtDispSchem_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
    Private Sub txtPack_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPack.KeyDown
        Try
            e.SuppressKeyPress = True
            SendKeys.Send("{TAB}")
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtPack_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtRate1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRate1.GotFocus
        Try
            If Trim(txtRate1.Text) <> String.Empty AndAlso CDbl(txtRate1.Text) = 0 Then txtRate1.Text = String.Empty
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtRate1_GotFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtRate1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRate1.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End If
            If BCommon.ValidateText(txtRate1.Text, e.KeyCode, "NUM") = False Then e.SuppressKeyPress = True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtRate1_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtAmount_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
        Try
            e.SuppressKeyPress = True
            SendKeys.Send("{TAB}")
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtAmount_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtMrp1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMrp1.GotFocus
        Try
            If Trim(txtMrp1.Text) <> String.Empty AndAlso CDbl(txtMrp1.Text) = 0 Then txtMrp1.Text = String.Empty
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtMrp1_GotFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtMrp1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMrp1.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End If
            If BCommon.ValidateText(txtMrp1.Text, e.KeyCode, "NUM") = False Then
                e.SuppressKeyPress = True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtMrp1_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmbMfg_CloseUp(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbMfg.CloseUp
        Try
            If Date.Today <= cmbMfg.Value Then
                BCommon.ShowMsgError(10119)
                cmbMfg.Value = Now.Date.AddDays(-1)
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbMfg_CloseUp")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmbMfg_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbMfg.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                If Date.Today <= cmbMfg.Value.Date() Then
                    BCommon.ShowMsgError(10119)
                    cmbMfg.Value = Now.Date.AddDays(-1)
                Else : SendKeys.Send("{TAB}")
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbMfg_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmbExp_CloseUp(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbExp.CloseUp
        Try
            If Date.Today > cmbExp.Value.Date() Then
                BCommon.ShowMsgError(10113)
                cmbExp.Value = BCommon.GetDatewithLastDay(Now.Date)
                cmbExp.Focus()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbExp_CloseUp")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmbExp_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbExp.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                If Date.Today > cmbExp.Value.Date() Then
                    BCommon.ShowMsgError(10113)
                    cmbExp.Value = BCommon.GetDatewithLastDay(Now.Date)
                Else
                    SendKeys.Send("{TAB}")
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cmbExp_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtScheme_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtScheme.LostFocus
        Try
            If Trim(txtScheme.Text) = String.Empty Then txtScheme.Text = 0
            Call SetDiscLesAmt()
            Call CalculateTaxes()
            If txtRate1.Text <> 0 Then PurItem.SchemeID = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtScheme_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtqty_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtqty.LostFocus
        Try
            If Trim(txtqty.Text) = String.Empty Then txtqty.Text = "0"
            Call CalculateTaxes()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtqty_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtRate1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRate1.LostFocus
        Try
            Dim dblMargin As Double
            If Trim(txtqty.Text) = String.Empty Then txtqty.Text = "0"
            If Trim(txtRate1.Text) = String.Empty Then
                txtRate1.Text = "0.00"
            Else
                txtRate1.Text = Format(CDbl(txtRate1.Text), "0.00")
            End If

            If CDbl(Trim(txtRate1.Text)) > CDbl(Trim(txtMrp1.Text)) Then
                BCommon.ShowMsgError(10284)
                txtRate1.Text = Format(txtMrp1.Text, "0.00")
                txtRate1.Focus()
                Me.Cursor = Cursors.Default
                Exit Sub
            End If
            If Trim(txtRate1.Text) <> String.Empty AndAlso CDbl(Trim(txtMrp1.Text)) <> 0 Then
                dblMargin = DProfitDetails.GetMaxMargin(PurItem.ItemID)
                If CDbl((CDbl(txtMrp1.Text) - CDbl(txtRate1.Text)) * 100 / CDbl(txtRate1.Text)) < dblMargin Then Call Display_Margins()
            End If

            txtAmount.Text = Format((CDbl(txtqty.Text) * CDbl(txtRate1.Text)), "0.00")
            Call CalculateTaxes()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtRate1_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txttax_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtTax.MouseClick
        Try
            If Trim(CDbl(txtRate1.Text)) <= 0 Then
                BCommon.ShowMsgError(10116)
                txtRate1.Focus()
                Me.Cursor = Cursors.Default
                Exit Sub
            Else
                Call CalculateTaxes()
                Call AssignTaxToGrid()
                Call ShowTaxGrid()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txttax_MouseClick")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtMargin_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMargin.GotFocus
        Try
            If Trim(txtMargin.Text) <> String.Empty OrElse CDbl(txtMargin.Text) = 0 Then txtMargin.Text = String.Empty
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtMargin_GotFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtMargin_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMargin.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                If Trim(txtMargin.Text) <> String.Empty AndAlso Trim(txtMargin.Text) > 0 Then
                    If Trim(txtMRP.Text) = String.Empty OrElse CDbl(Trim(txtMRP.Text)) < 0 Then txtMRP.Text = "0.00"
                    If Trim(txtRate.Text) = String.Empty OrElse CDbl(Trim(txtRate.Text)) < 0 Then txtRate.Text = "0.00"
                    txtRate.Text = Math.Round(((CDbl(txtMRP.Text) * 100) / (CDbl(txtMargin.Text) + 100)), 2)
                End If
                SendKeys.Send("{TAB}")
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtMargin_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtMRP_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMRP.GotFocus
        Try
            If Trim(txtMRP.Text) <> String.Empty AndAlso CDbl(txtMRP.Text) = 0 Then txtMRP.Text = String.Empty
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtMRP_GotFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtMRP_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMRP.LostFocus
        Try
            If Trim(txtMRP.Text) = String.Empty Then
                txtMRP.Text = "0.00"
            Else
                txtMRP.Text = Format(CDbl(txtMRP.Text), "0.00")
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtMRP_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtRate_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRate.GotFocus
        Try
            If Trim(txtRate.Text) <> String.Empty AndAlso CDbl(txtRate.Text) = 0 Then txtRate.Text = String.Empty
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtRate_GotFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtRate_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRate.LostFocus
        Try
            If Trim(txtRate.Text) <> String.Empty AndAlso Trim(txtRate.Text) > 0 Then
                If Trim(txtMRP.Text) = String.Empty OrElse CDbl(Trim(txtMRP.Text)) < 0 Then txtMRP.Text = "0.00"
                txtMargin.Text = Format(Math.Round(((CDbl(txtMRP.Text) - CDbl(txtRate.Text)) / CDbl(txtRate.Text)) * 100, 2), "0.00")
            Else
                txtRate.Text = "0.00"
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtRate_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtMargin_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMargin.LostFocus
        Try
            If Trim(txtMargin.Text) <> String.Empty AndAlso Trim(txtMargin.Text) > 0 Then
                If Trim(txtMRP.Text) = String.Empty OrElse CDbl(Trim(txtMRP.Text)) < 0 Then txtMRP.Text = "0.00"
                If Trim(txtRate.Text) = String.Empty OrElse CDbl(Trim(txtRate.Text)) < 0 Then txtRate.Text = "0.00"
                txtRate.Text = Format(Math.Round(((CDbl(txtMRP.Text) * 100) / (CDbl(txtMargin.Text) + 100)), 2), "0.00")
            Else
                txtMargin.Text = "0.00"
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtMargin_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtMRP_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMRP.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then
                SendKeys.Send("{TAB}")
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtMRP_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtRate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtRate.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                If Trim(txtRate.Text) <> String.Empty AndAlso Trim(txtRate.Text) > 0 Then
                    If Trim(txtMRP.Text) = String.Empty OrElse CDbl(Trim(txtMRP.Text)) < 0 Then txtMRP.Text = "0.00"
                    txtMargin.Text = Math.Round(((CDbl(txtMRP.Text) - CDbl(txtRate.Text)) / CDbl(txtRate.Text)) * 100, 2)
                End If
                SendKeys.Send("{TAB}")
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtRate_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtDisc_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDisc.LostFocus
        Try
            If Trim(txtDisc.Text) = String.Empty Then txtDisc.Text = "0.00"
            Call CalculateTaxes()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtDisc_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtMrp1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMrp1.LostFocus
        Try
            If Trim(txtMrp1.Text) = String.Empty Then
                txtMrp1.Text = "0.00"
            Else
                txtMrp1.Text = Format(CDbl(txtMrp1.Text), "0.00")
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtMrp1_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtBatchNo_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBatchno.LostFocus
        Dim dtbTbatch As DataTable
        Try
            If Trim(txtBatchno.Text) <> String.Empty Then
                dtbTbatch = DPurchaseInvoice.BatchDetail(ItemID, txtBatchno.Text)
                If dtbTbatch.Rows.Count > 0 Then
                    txtMrp1.Enabled = False
                    cmbMfg.Value = dtbTbatch.Rows(0).Item(0)
                    cmbMfg.CustomFormat = "MMM/yy"
                    cmbMfg.Enabled = False
                    cmbExp.Value = dtbTbatch.Rows(0).Item(1)
                    cmbExp.CustomFormat = "MMM/yy"
                    cmbExp.Enabled = False
                    txtMrp1.Text = Format(dtbTbatch.Rows(0).Item(2), "0.00")
                Else
                    txtMrp1.Enabled = True
                    cmbMfg.Enabled = True
                    cmbExp.Enabled = True
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtBatchno_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

#End Region '"FORM EVENTS & CONTROL EVENTS"

#Region "USER DEFINED FUNCTIONS"
    Private Sub DisplayData()
        Try
            If Not IsNothing(ItemScheme) AndAlso ItemScheme.SchemeID > 0 Then
                Call CalculateSchemeAmount()
            End If
            Call CalculateTaxes()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Public Sub Display_Margins()
        MarginStatus = "T"
        btnOkShow.Enabled = False
        btnClose.Enabled = False
        Dim dtbMargin As DataTable
        Dim intCount As Integer
        Try
            If VoucherType = "DP" Then txtMRP.Enabled = False
            lblAlert.Text = String.Empty
            GrpbMargin.Visible = True
            GrpbMargin.Location = New Point(lblPack.Left, txtPack.Bottom)
            cfgMargins.Visible = True
            cfgMargins.Focus()
            txtMargin.Focus()

            With cfgMargins
                .Cols.Count = 8
                .Cols(0).Caption = "Item ID"
                .Cols(1).Caption = "Margin (%)"
                .Cols(2).Caption = "MRP"
                .Cols(3).Caption = "Bill Rate"
                .Cols(4).Caption = "Sale Price"
                .Cols(4).Visible = False
                .Cols(5).Caption = "VendorID"
                .Cols(6).Caption = "Vendor Name"
                .Cols(7).Caption = "Invoice Date"
                .Cols(7).Format = "dd/MM/yy"
                .Cols(1).DataType = System.Type.GetType("System.Int32")
                .Cols(0).Width = 0                  'Item ID
                .Cols(0).Visible = False
                .Cols(1).Width = .Width() / 6       'Margin
                .Cols(2).Width = .Width() / 6       'MRP
                .Cols(3).Width = .Width() / 4.5     'Purchase Price
                .Cols(4).Width = .Width() / 6       'SalePrice
                .Cols(5).Width = 0                  'VendorID 
                .Cols(5).Visible = False
                .Cols(6).Width = .Width() / 4       'Vendor Name
                .Cols(7).Width = .Width() / 11      'Invoice Date

                dtbMargin = DProfitDetails.GetDetails(ItemID)

                If dtbMargin.Rows.Count > 0 Then
                    .Rows.Count = dtbMargin.Rows.Count + 1
                    For intCount = 0 To dtbMargin.Rows.Count - 1
                        .Item(intCount + 1, 0) = dtbMargin.Rows(intCount).Item(0)         'ITEM ID
                        .Item(intCount + 1, 1) = dtbMargin.Rows(intCount).Item(2)         '"MARGIN"
                        .Item(intCount + 1, 2) = dtbMargin.Rows(intCount).Item(3)         '"MRP"
                        .Item(intCount + 1, 3) = dtbMargin.Rows(intCount).Item(4)         '"Purchase Price"
                        .Item(intCount + 1, 4) = dtbMargin.Rows(intCount).Item(5)         '"SalePrice"
                        .Item(intCount + 1, 5) = dtbMargin.Rows(intCount).Item(7)         '"VendorID"
                        .Item(intCount + 1, 6) = dtbMargin.Rows(intCount).Item(8)         '"Vendor Name"
                        .Item(intCount + 1, 7) = CType(dtbMargin.Rows(intCount).Item(10), Date)        '"Invoice Date"
                    Next
                Else
                    txtMargin.Text = "0.00"
                    txtRate1.Text = "0.00"
                    txtMrp1.Text = "0.00"
                End If

                If Trim(txtMrp1.Text) <> String.Empty Then txtMRP.Text = Format(CDbl(txtMrp1.Text), "0.00")

                If Trim(txtRate1.Text) <> String.Empty Then txtRate.Text = Format(CDbl(txtRate1.Text), "0.00")

                If (Trim(txtMrp1.Text) <> String.Empty AndAlso CDbl(Trim(txtMrp1.Text)) > 0) Then
                    If (Trim(txtRate1.Text) <> String.Empty AndAlso CDbl(Trim(txtRate1.Text)) > 0) Then
                        txtMargin.Text = Math.Round(((CDbl(txtMRP.Text) - CDbl(txtRate.Text)) / CDbl(txtRate.Text)) * 100, 2)
                    End If
                Else : txtMargin.Text = "0.00"
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Display_Margins")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Public Sub Show_Scheme()
        Dim frmSelectScheme As New frmSchemeSelection
        dblTotSchmDisc = 0
        Try
            With frmSelectScheme
                If PurItem.SchemeID > 0 Then ._intSchemeID = PurItem.SchemeID
                ._intItemID = PurItem.ItemID
                .dtInvoicedate = Now.Date()
                If VoucherType = "DP" Then
                    .strDC = "DP"
                End If
                .ShowDialog()
                '.Location = New Point(lblScheme.Left, txtScheme.Bottom)
                If ._intSchemeID > 0 Then
                    PurItem.SchemeID = ._intSchemeID
                    txtScheme.Focus()
                Else
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End With

            ItemScheme = BScheme.GetScheme(PurItem.SchemeID)
            Call CalculateSchemeAmount()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Show_Scheme")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub CalculateSchemeAmount()
        Try
            If IsNothing(ItemScheme) Then Me.Cursor = Cursors.Default : Exit Sub
            With ItemScheme
                If .SchemeID = 0 Then Me.Cursor = Cursors.Default : Exit Sub
                txtDispSchem.Text = BScheme.GetSchemeName(ItemScheme)
                If .schemeItemID = 0 Then  'FREE ITEM ON ITEM
                    If .DiscountType = "A" Then 'DISCOUNT IN AMOUNT
                        dblTotSchmDisc = .Discount * CDbl(txtqty.Text)
                        txtScheme.Text = Format(dblTotSchmDisc, "0.00")
                    ElseIf .DiscountType = "P" Then 'DISCOUNT IN PERCENTAGE
                        dblTotSchmDisc = CDbl(txtRate1.Text) * (CDbl(.Discount / 100)) * CDbl(txtqty.Text)
                        txtScheme.Text = Format(dblTotSchmDisc, "0.00")
                    End If
                Else
                    txtScheme.Text = BScheme.GetSchemeQty(.SchemeID, CDbl(txtqty.Text))
                End If
                PurItem.SchemeDiscount = dblTotSchmDisc
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "CalculateSchemeAmount")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Public Sub SetDiscLesAmt()
        Try
            If Trim(txtqty.Text) = "" Then txtqty.Text = "0"
            If Trim(txtRate1.Text) = "" Then txtRate1.Text = "0.00"
            If Trim(txtDisc.Text) = "" Then txtDisc.Text = "0.00"

            txtAmount.Text = Format((CDbl(txtqty.Text) * CDbl(txtRate1.Text)), "0.00")
            Call CalculateSchemeAmount()
            If IsNothing(ItemScheme) Then
                DiscLessAmt = txtAmount.Text - IIf(Trim(txtDisc.Text) = String.Empty, 0, CDbl(txtDisc.Text))
                Me.Cursor = Cursors.Default
                Exit Sub
            Else
                If Not IsNothing(ItemScheme.SchemeType) AndAlso ItemScheme.SchemeType <> "F" Then

                    DiscLessAmt = txtAmount.Text - (CDbl(txtDisc.Text) + CDbl(txtScheme.Text))

                Else : DiscLessAmt = txtAmount.Text - CDbl(txtDisc.Text)
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetDiscLesAmt")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub setControls()
        Try
            dtbDefltTaxID = BTax.GetDefaultTaxID()
            If VoucherType = "DP" Then
                If ItemScheme.SchemeID <> 0 Then
                    txtDispSchem.Enabled = False
                    txtScheme.Enabled = False
                End If
                txtMrp1.Enabled = False
                txtBatchno.Enabled = False
                cmbMfg.Enabled = False
                cmbExp.Enabled = False
                txtqty.Enabled = False
                txtPack.Enabled = False
                txtAmount.Enabled = False
                txtScheme.MaxLength = 6
                txtRate1.MaxLength = 13
                txtDisc.MaxLength = 13
            ElseIf VoucherType = "PI" Then
                txtAmount.Enabled = False
                txtBatchno.MaxLength = 20
                txtqty.MaxLength = 6
                txtScheme.MaxLength = 6
                txtMrp1.MaxLength = 13
                txtRate1.MaxLength = 13
                txtDisc.MaxLength = 13
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "setControls")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function ValidateData() As Byte
        Try
            If Date.Today > cmbExp.Value.Date() Then
                BCommon.ShowMsgError(10113)
                cmbExp.Value = BCommon.GetDatewithLastDay(Now.Date)
                cmbExp.Focus()
                Return 0
            ElseIf Date.Today <= cmbMfg.Value Then
                BCommon.ShowMsgError(10119)
                cmbMfg.Value = Now.Date.AddDays(-1)
                cmbExp.Focus()
                Return 0
            ElseIf Trim(txtBatchno.Text) = String.Empty Then
                BCommon.ShowMsgError(10112)
                txtBatchno.Focus()
                Return 0
            ElseIf CDbl(Trim(txtqty.Text)) <= 0 Then
                BCommon.ShowMsgError(10232)
                txtqty.Focus()
                Return 0
            ElseIf VoucherType <> "DC" Then
                If CDbl(Trim(txtMrp1.Text)) <= 0 Then
                    BCommon.ShowMsgError(10115)
                    txtMrp1.Focus()
                    Return 0
                    'BILL RATE
                ElseIf CDbl(Trim(txtRate1.Text)) <= 0 Then
                    BCommon.ShowMsgError(10116)
                    txtRate1.Focus()
                    Return 0
                End If
            End If
            If Not IsNothing(ItemScheme) AndAlso ItemScheme.SchemeID > 0 AndAlso ItemScheme.schemeItemID = 0 Then
                If CDbl(txtAmount.Text) - CDbl(txtDisc.Text) - CDbl(txtScheme.Text) <= 0 Then
                    BCommon.ShowMsgError(10337)
                    Return 0
                End If
            Else
                If CDbl(txtAmount.Text) - CDbl(txtDisc.Text) <= 0 Then
                    BCommon.ShowMsgError(10337)
                    Return 0
                End If
            End If

            'SAME ITEM-BATCH
            With frmNewPI.fgItems
                For intCnt As Integer = 1 To .Rows.Count - 1
                    If intCnt = .Row Then Continue For
                    If IsNothing(.Item(intCnt, 0)) OrElse Trim(.Item(intCnt, 0)) = String.Empty Then Exit For
                    If Not IsNothing(.Item(intCnt, 5)) AndAlso Trim(.Item(intCnt, 5)) <> String.Empty Then
                        If Trim(.Item(intCnt, 5)) = Trim(txtBatchno.Text) AndAlso Trim(.Item(intCnt, 0)) = PurItem.ItemID Then
                            BCommon.ShowMsgError(10336)
                            txtBatchno.Focus()
                            Return 0
                        End If
                    End If
                Next
            End With

            Call CalculateTaxes()
            If Not IsNothing(ItemScheme) AndAlso ItemScheme.schemeItemID > 0 Then
                If (((CDbl(txtAmount.Text) + CDbl(dblTotalTax)) - (CDbl(txtDisc.Text))) / CDbl(txtqty.Text)) > CDbl(txtMrp1.Text) Then
                    BCommon.ShowMsgError(10341)
                    txtMrp1.Focus()
                    Return 0
                End If
            Else
                If (((CDbl(txtAmount.Text) + CDbl(dblTotalTax)) - (CDbl(txtScheme.Text) + CDbl(txtDisc.Text))) / CDbl(txtqty.Text)) > CDbl(txtMrp1.Text) Then
                    BCommon.ShowMsgError(10341)
                    txtMrp1.Focus()
                    Return 0
                End If
            End If
            Return 1
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Public Sub CalculateTaxes()
        Try
            dblTotalTax = 0
            Call SetDiscLesAmt()
            If IsNothing(ItemTaxes) Then Me.Cursor = Cursors.Default : Exit Sub
            For intCount As Integer = 0 To ItemTaxes.Length - 1
                'Rahul_20080304 : Add CODE for TAX for SALE on PI
                If ItemTaxes(intCount).ApplyTo.ToString = dtbDefltTaxID.Rows(1).Item(1).ToString OrElse _
                   ItemTaxes(intCount).ApplyTo.ToString = dtbDefltTaxID.Rows(4).Item(1).ToString Then
                    ItemTaxes(intCount).TaxAmount = 0
                    dblTotalTax = dblTotalTax + CDbl(ItemTaxes(intCount).TaxAmount)
                Else
                    ItemTaxes(intCount).TaxAmount = ItemTaxes(intCount).TaxValue * CDbl(DiscLessAmt) / 100
                    dblTotalTax = dblTotalTax + CDbl(ItemTaxes(intCount).TaxAmount)
                End If
            Next
            txtTax.Text = GetTaxString()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "CalculateTaxes")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function CanFormClose() As Boolean
        Try
            If cfgTax.Visible = True Then
                Return False
            ElseIf GrpbMargin.Visible = True Then
                Return False
            End If
            Return True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "CanFormClose")
            Me.Cursor = Cursors.Default
            Return False
        End Try
    End Function

    Public Sub ShowTaxGrid()
        Try
            If GrpbMargin.Visible = True Then
                btnCancel1_Click(Nothing, Nothing)
            End If

            If IsNothing(ItemTaxes) OrElse ItemTaxes.Length <= 0 Then
                BCommon.ShowMsgError(10333)
                txtTax.Text = "0.00"
            Else
                With cfgTax
                    .Visible = True
                    .Col = 2
                    .Row = 1
                    .Focus()
                End With

            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ShowTaxGrid")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Public Sub AssignTaxToGrid()
        Try
            With cfgTax
                If IsNothing(ItemTaxes) Then Me.Cursor = Cursors.Default : Exit Sub
                If ItemTaxes.Length <= 0 Then Me.Cursor = Cursors.Default : Exit Sub
                .Rows.Count = ItemTaxes.Length + 1
                For intCount As Integer = 0 To ItemTaxes.Length - 1
                    .Item(intCount + 1, 0) = ItemTaxes(intCount).TaxID
                    .Item(intCount + 1, 1) = ItemTaxes(intCount).TaxName
                    .Item(intCount + 1, 2) = ItemTaxes(intCount).TaxValue
                    'Rahul_20080304 : Add CODE for TAX for SALE on PI
                    If ItemTaxes(intCount).ApplyTo.ToString = dtbDefltTaxID.Rows(1).Item(1).ToString OrElse _
                       ItemTaxes(intCount).ApplyTo.ToString = dtbDefltTaxID.Rows(4).Item(1).ToString Then
                        .Item(intCount + 1, 3) = 0
                    Else
                        .Item(intCount + 1, 3) = ItemTaxes(intCount).TaxAmount
                    End If
                    .Item(intCount + 1, 4) = ItemTaxes(intCount).TaxValue
                    .Item(intCount + 1, 5) = ItemTaxes(intCount).ApplyTo
                Next
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "AssignTaxToGrid")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Public Sub GetTaxFromGrid()
        Try
            With cfgTax
                If IsNothing(ItemTaxes) Then Me.Cursor = Cursors.Default : Exit Sub
                If .Rows.Count <= 1 Then Me.Cursor = Cursors.Default : Exit Sub
                For intCount As Integer = 0 To ItemTaxes.Length - 1
                    ItemTaxes(intCount).TaxID = .Item(intCount + 1, 0)
                    ItemTaxes(intCount).TaxName = .Item(intCount + 1, 1)
                    ItemTaxes(intCount).TaxValue = .Item(intCount + 1, 2)
                    ItemTaxes(intCount).TaxAmount = .Item(intCount + 1, 3)
                    ItemTaxes(intCount).ApplyTo = .Item(intCount + 1, 5)
                Next
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "GetTaxFromGrid")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Public Sub SetDefualtTaxes()
        Try
            ItemTaxes = BTax.GetItemTaxes(PurItem.ItemID, PurItem.CLRcode)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetDefualtTaxes")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub SetTaxGrid()
        Try
            With cfgTax

                .Cols.Count = 6
                .Rows.Count = 2
                .Rows.Fixed = 1

                'CAPTIONS
                .Cols(0).Caption = "Tax ID"
                .Cols(1).Caption = "Name"
                .Cols(2).Caption = "Rate(%)"
                .Cols(3).Caption = "Amount"
                .Cols(4).Caption = "Rate1"
                .Cols(5).Caption = "Apply To"

                'DATATYPE
                .Cols(1).DataType = System.Type.GetType("System.Text")
                .Cols(2).DataType = System.Type.GetType("System.Decimal")
                .Cols(3).DataType = System.Type.GetType("System.Decimal")
                .Cols(4).DataType = System.Type.GetType("System.Decimal")
                .Cols(5).DataType = System.Type.GetType("System.Text")

                'LOACATION ,SIZE
                If VoucherType = "PI" Then
                    .Location = New Point(txtTax.Left, txtTax.Bottom + 10)
                ElseIf VoucherType = "DP" Then
                    .Location = New Point(txtTax.Bottom + 10, txtTax.Bottom + 10)
                End If
                .Width = 278
                .Cols(0).Width = 0
                .Cols(1).Width = 100
                .Cols(2).Width = 55
                .Cols(3).Width = 55
                .Cols(4).Width = 0
                .Cols(5).Width = 65

                .Cols(0).Visible = False
                .Cols(4).Visible = False
                .Cols(1).Visible = True
                .Cols(2).Visible = True
                .Cols(3).Visible = True
                .Cols(5).Visible = True

                .Cols(1).AllowEditing = False
                .Cols(2).AllowEditing = True
                .Cols(3).AllowEditing = True
                .Cols(4).AllowEditing = False
                .Cols(5).AllowEditing = False

                .Cols(3).TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.RightBottom

            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetTaxGrid")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Public Function GetTaxString() As String
        Dim strTax As New StringBuilder
        Dim intCount As Integer
        Try
            For intCount = 0 To ItemTaxes.Length - 1
                If ItemTaxes(intCount).TaxValue > 0 Then strTax.Append(ItemTaxes(intCount).TaxName & "=" & ItemTaxes(intCount).TaxAmount & ",")
            Next
            If strTax.Length > 0 Then Return strTax.Replace(",", String.Empty, strTax.Length - 1, 1).ToString()
            Return String.Empty
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "GetTaxString")
            Me.Cursor = Cursors.Default
            Return String.Empty
        End Try
    End Function
#End Region '"USER DEFINED FUNCTIONS"

#Region "GRID EVENT FUNCTION"

    Private Sub cfgTax_BeforeEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgTax.BeforeEdit
        Try
            With cfgTax
                Select Case .Col
                    Case 4
                        If Not (IsNothing(.Item(.Row, 2)) OrElse Trim(.Item(.Row, 2)) = "" OrElse .Item(.Row, 2) <= 0) _
                            AndAlso (ItemTaxes(.Item(.Row, 2)).ApplyTo.ToString = dtbDefltTaxID.Rows(1).Item(1).ToString() _
                            OrElse ItemTaxes(.Item(.Row, 2)).ApplyTo.ToString = dtbDefltTaxID.Rows(4).Item(1).ToString()) Then
                            e.Cancel = True
                        End If
                End Select
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgTax_BeforeEdit")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgTax_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cfgTax.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                If cfgTax.Row = cfgTax.Rows.Count - 1 And cfgTax.Col = cfgTax.Cols.Count - 1 Then cfgTax.Visible = False : txtTax.Focus()
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgTax_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgTax_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cfgTax.KeyUp
        Try
            If e.KeyCode = Keys.Escape Then cfgTax.Visible = False
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgTax_KeyUp")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgTax_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cfgTax.LostFocus
        Try
            cfgTax.Visible = False
            Call GetTaxFromGrid()
            Call CalculateTaxes()
            If txtRate1.Text <> 0 Then PurItem.SchemeID = 0
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgTax_LostFocus")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgTax_AfterEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles cfgTax.AfterEdit
        Try
            Call SetDiscLesAmt()
            With cfgTax
                Select Case .Col
                    Case 2
                        If Not (.Item(.Row, 5) = dtbDefltTaxID.Rows(1).Item(1).ToString() OrElse _
                                .Item(.Row, 5) = dtbDefltTaxID.Rows(4).Item(1).ToString()) Then
                            .Item(.Row, 3) = .Item(.Row, 2) * CDbl(DiscLessAmt) / 100
                            .Item(.Row, 4) = .Item(.Row, 2)
                        Else
                            .Item(.Row, 3) = 0
                        End If
                    Case 3
                        If Not (.Item(.Row, 5) = dtbDefltTaxID.Rows(1).Item(1).ToString() OrElse _
                                .Item(.Row, 5) = dtbDefltTaxID.Rows(4).Item(1).ToString()) Then
                            .Item(.Row, 2) = (100 / CDbl(DiscLessAmt)) * .Item(.Row, 3)
                            .Item(.Row, 4) = .Item(.Row, 2)
                        Else
                            .Item(.Row, 3) = 0
                        End If
                End Select
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgTax_AfterEdit")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cfgMargins_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles cfgMargins.DoubleClick
        Dim Rate As Double
        Try
            With cfgMargins
                If .Rows.Count > 1 Then
                    If Not IsNothing(.Item(.Row, 1)) AndAlso .Item(.Row, 1) <> 0 Then txtMargin.Text = Format(.Item(.Row, 1), "0.00")
                    If CDbl(txtMRP.Text) > 0 Then
                        Rate = ((txtMRP.Text * 100) / (txtMargin.Text + 100))
                        txtRate.Text = Format(Rate, "0.00")
                    End If
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "cfgMargins_DoubleClick")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
#End Region '"GRID EVENT FUNCTION"

#Region "BUTTON EVENTS"
    Private Sub btnshow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnshow.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            txtMrp1.Text = Format(CDbl(txtMRP.Text), "0.00")
            txtRate1.Text = Format(CDbl(txtRate.Text), "0.00")
            GrpbMargin.Visible = False
            MarginStatus = "F"
            btnOkShow.Enabled = True
            btnClose.Enabled = True
            btnOkShow.Focus()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnshow_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnCancel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel1.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            GrpbMargin.Visible = False
            MarginStatus = "F"
            btnOkShow.Enabled = True
            btnClose.Enabled = True
            btnshow.Focus()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnCancel1_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnOkShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOkShow.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If ValidateData() = 0 Then Me.Cursor = Cursors.Default : Exit Sub
            Status = "O"
            Me.Close()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnOkShow_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Try
            Status = "C"
            Me.Close()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnClose_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
#End Region '"BUTTON EVENTS"      

End Class